﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DNA.Models;
using System.IO;

namespace DNA.Controllers
{ 
    public class DNAController : Controller
    {
        private DNAAnalysisEntities db = new DNAAnalysisEntities();

        //
        // GET: /DNA/

        public ViewResult Index()
        {
            return View(db.DNA.ToList());
        }

        //
        // GET: /DNA/Details/5

        public ViewResult Details(int id)
        {
            DNA.Models.DNA dna = db.DNA.Single(d => d.Id == id);
            return View(dna);
        }

        //
        // GET: /DNA/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /DNA/Create

        [HttpPost]
        public ActionResult Create(DNA.Models.DNA dna)
        {
            if (ModelState.IsValid)
            {
                //Pega os dados do usuario Logado
                int userId = Convert.ToInt32(Session["UserId"].ToString());
                DNA.Models.User user = db.User.Single(u => u.Id == userId);
                //Add a FK o id do usuario
                dna.User = user;

                //Prepara o armazenamento do arquivo de dna
                foreach (string fileName in Request.Files)
                {
                    HttpPostedFileBase postedFile = Request.Files[fileName];
                    
                    postedFile.SaveAs(Server.MapPath("~/DNAFiles/") + Path.GetFileName(postedFile.FileName) + "-DNAUser-"+ user.Id.ToString() + ".txt");
                    dna.CompleteDNASequence =  postedFile.FileName + "-DNAUser-" + user.Id.ToString() + ".txt";
                }
                
                db.DNA.AddObject(dna);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(dna);
        }
        
        //
        // GET: /DNA/Edit/5
 
        public ActionResult Edit(int id)
        {
            DNA.Models.DNA dna = db.DNA.Single(d => d.Id == id);
            return View(dna);
        }

        //
        // POST: /DNA/Edit/5

        [HttpPost]
        public ActionResult Edit(DNA.Models.DNA dna)
        {
            if (ModelState.IsValid)
            {
                db.DNA.Attach(dna);
                db.ObjectStateManager.ChangeObjectState(dna, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(dna);
        }

        //
        // GET: /DNA/Delete/5
 
        public ActionResult Delete(int id)
        {
            DNA.Models.DNA dna = db.DNA.Single(d => d.Id == id);
            return View(dna);
        }

        //
        // POST: /DNA/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            DNA.Models.DNA dna = db.DNA.Single(d => d.Id == id);
            db.DNA.DeleteObject(dna);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }


        public FileResult Download(int id)
        {
            DNA.Models.DNA dna = db.DNA.Single(d => d.Id ==id);

            string path = Server.MapPath("~/DNAFiles/") + dna.CompleteDNASequence;
            FileStream stream = new FileStream(path, FileMode.Open);
            return File(stream,"details", dna.CompleteDNASequence); 
        }
    }
}